/**
 * OSS is a Open Service Container which gives the leverage of building the Desktop and Web
 * Application. * Copyright (C) 2008-2009  Ponraj Sutanthiramani
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Or5e Open Source Community
 * 105, S.S Paradise Apartment, Munnireddy Layout, Horamavu, Banaswadi, Bangalore, India.
 */
package org.or5e.api.mp;

import java.io.File;

import org.or5e.api.mp.player.audio.AudioPlayer;
import org.or5e.api.mp.player.audio.PlaybackEvent;
import org.or5e.api.mp.player.audio.PlaybackListener;
import org.or5e.api.mp.player.audio.PlayerException;
import org.or5e.api.mp.playlist.Playlist;
import org.or5e.api.mp.playlist.PlaylistItem;
import org.or5e.core.BaseObject;

/**
 * <font face='verdana'>
 * Default Media listener for the playback functionality of the API. This users
 * XtreamEmp API (Licensed GNU). This handles the functionality of moving to the
 * next song if the current player reaches the end of the track.<br/>
 * Again it acts as a adaptor class for the API Users can hold of other operations.
 *
 * @since 0.1b
 * @version 1.0b
 * @author Ponraj Suthanthriamani
 * </font>
 */
public abstract class MediaPlaybackListener extends BaseObject implements PlaybackListener {
    private AudioPlayer _player = null;
    private Playlist _playList = null;
    public MediaPlaybackListener(Playlist playList, AudioPlayer play) {
        this._player = play;
        this._playList = playList;
    }

    @Override
    public final void playbackEndOfMedia(PlaybackEvent pe) {
        // If the current track reaches the end of media player should start the
        // next track without any user intervension.
        _playList.nextCursor();
        PlaylistItem pli = _playList.getCursor();
        if(pli != null) {
            try {
                _player.open(new File(pli.getLocation()));
                _player.setGain(Settings.getGain() / 100f);
                _player.play();
            }
            catch (PlayerException ex) {
                error(ex);
            }
        }
    }
}